package com.itxiuyixiu.homework.admin.service;

import com.itxiuyixiu.homework.entity.dao.RewardsAndPunishmentDao;
import com.itxiuyixiu.homework.entity.dao.StudentDao;
import com.itxiuyixiu.homework.entity.dto.RewardsAndPunishmentDto;
import com.itxiuyixiu.homework.entity.model.RewardsAndPunishment;
import com.itxiuyixiu.homework.entity.model.Student;
import com.itxiuyixiu.homework.entity.vo.RewardsAndPunishmentVo;
import com.itxiuyixiu.homework.utils.ReturnUtils.ReturnVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author 黄磊
 * @date 2020/3/9
 */
@Service
public class AdminRewardsAndPublishService {
    private final StudentDao studentDao;
    private final RewardsAndPunishmentDao rewardsAndPunishmentDao;

    public AdminRewardsAndPublishService(StudentDao studentDao, RewardsAndPunishmentDao rewardsAndPunishmentDao) {
        this.studentDao = studentDao;
        this.rewardsAndPunishmentDao = rewardsAndPunishmentDao;
    }

    /**
     * 查询奖惩记录信息
     *
     * @param dto 查询信息（只有id、studentId、rewarded有效）
     * @return 奖惩记录信息
     */
    public List<RewardsAndPunishmentVo> getRewardsAndPunishmentMessage(RewardsAndPunishmentDto dto) {
        if (dto.getId() != null) {
            return rewardsAndPunishmentDao.findAllById(dto.getId());
        } else if (dto.getStudentId() !=null && !"".equals(dto.getStudentId())) {
            return rewardsAndPunishmentDao.findAllByStudentId(dto.getStudentId());
        } else if (dto.getRewarded() != null) {
            return rewardsAndPunishmentDao.findAllByRewarded(dto.getRewarded());
        }
        return rewardsAndPunishmentDao.findAllRewardsAndPunishment();
    }

    /**
     * 添加奖惩记录
     *
     * @param dto 奖惩记录信息
     * @return 状态（0-添加成功，1-学生不存在）
     */
    public int addRecord(RewardsAndPunishmentDto dto) {
        Optional<Student> studentOptional = studentDao.findById(dto.getStudentId());
        if (!studentOptional.isPresent()){
            return 1;
        }
        Student student = studentOptional.get();
        RewardsAndPunishment rewardsAndPunishment = RewardsAndPunishmentDto.parseRewardsAndPunishment(dto, student);
        rewardsAndPunishmentDao.save(rewardsAndPunishment);
        return 0;
    }

    /**
     * 修改奖惩记录
     *
     * @param recordId 奖惩记录id
     * @param dto 奖惩信息
     * @return 状态（0-修改成功，1-记录不存在，2-学生不存在）
     */
    public int modifyRecord(int recordId , RewardsAndPunishmentDto dto){
        Optional<RewardsAndPunishment> recordOptional = rewardsAndPunishmentDao.findById(recordId);
        if (!recordOptional.isPresent()) {
            return 1;
        }
        Optional<Student> studentOptional = studentDao.findById(dto.getStudentId());
        if (!studentOptional.isPresent()){
            return 2;
        }
        Student student = studentOptional.get();
        RewardsAndPunishment record = recordOptional.get();
        record.setTitle(dto.getTitle());
        record.setDetail(dto.getDetail());
        record.setTime(dto.getTime());
        record.setRewarded(dto.getRewarded());
        record.setStudent(student);
        rewardsAndPunishmentDao.save(record);
        return 0;
    }

    /**
     * 删除记录
     *
     * @param recordId 记录Id
     * @return 状态（true-成功删除，false-记录不存在）
     */
    public boolean deleteRecord(int recordId) {
        if (!rewardsAndPunishmentDao.existsById(recordId)) {
            return false;
        }
        rewardsAndPunishmentDao.deleteById(recordId);
        return true;
    }
}
